//
//  LCBaseVC.m
//  stone-story
//
//  Created by dodo liu on 12-9-25.
//  Copyright (c) 2012年 liu dodo. All rights reserved.
//

#import "LCBaseVC.h"

@interface LCBaseVC ()

@end

@implementation LCBaseVC
//@synthesize _queue;
@synthesize HUD;
@synthesize resultArray;
@synthesize rootNodeName;
@synthesize lcalertview;
@synthesize haveConnect;
@synthesize APPDELE;
@synthesize navTitleLabel;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setViewBackground];
    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"baseBackView.jpg"]]];
    //APPDELE=[[UIApplication sharedApplication]delegate];
    
    
}

- (void)setViewBackground
{
    [self.view setBackgroundColor:[UIColor grayColor]];
}


-(void)setNavItemColor
{
    
    navTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(160, 0, 160, 30)];
    navTitleLabel.textColor = [UIColor colorWithRed:140.0/255 green:185.0/255 blue:239.0/255 alpha:1.0];
    navTitleLabel.backgroundColor = [UIColor clearColor];
    [navTitleLabel setTextAlignment:UITextAlignmentCenter];
    //NAVBARTITLE(@"香榭站");
    [navTitleLabel setFont:[UIFont fontWithName:@"Thonburi-Bold" size:20.0]];
    [navTitleLabel setCenter:CGPointMake(160, 22)];
    [self.navigationItem setTitleView:navTitleLabel];
    [self.navigationController.navigationBar setBarStyle:UIBarStyleBlack];
    
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark init
- (void)resultArray_init
{
    resultArray =[[NSMutableArray alloc]init];
}

#pragma mark HUD
//create
- (void)HUD_Create:(UIView*)view
{
    HUD = [[MBProgressHUD alloc] initWithView:view];
    [view addSubview:HUD];
    [view bringSubviewToFront:HUD];
    HUD.delegate = self;
    HUD.labelText =@"正在连接...";
    
}
//show
- (void)HUD_Show
{
    [HUD show:YES]; 
}
//hide
- (void)HUD_Hide
{
    [HUD hide:YES afterDelay:1];
}

#pragma mark - 数据请求
- (void)request_Send:(NSString*)urlStr withPostArg:(NSMutableArray*)argArray
{
    
    NSURL *url = [NSURL URLWithString:urlStr];
    //if(argArray)
   // { ASIFormDataRequest *request=[[ASIFormDataRequest alloc] initWithURL:url];}
   // else
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    //超时时间
    request.timeOutSeconds = 30;
    //添加参数值
    for(int i=0;i<argArray.count;i=i+2)
    {
        NSString *postValue=[argArray objectAtIndex:i];
        NSString *postKey=[argArray objectAtIndex:i+1]; 
        NSLog(@"postvalue:[%@],postkey:[%@]",postValue,postKey);
       // [request addPostValue: postValue  forKey:postKey];
    }
    //定义异步方法
    [request setDelegate:self];
    [request setDidFailSelector:@selector(requestDidFailed:)];
    [request setDidFinishSelector:@selector(requestDidSuccess:)];
    //开始执行
    [request startAsynchronous];
    
    [self resultArray_init];
    
}

- (void)requestDidSuccess:(ASIFormDataRequest *)request
{
    NSData *XMLData =[request responseData];
    
    //生成CXMLDocument对象：
    CXMLDocument *document = [[CXMLDocument alloc] initWithData:XMLData options:0 error:nil];
    //使用parseDire来解析(推荐):
    [self parseDire:document];
    //NSLog(@"%@",[request responseString]);
}

//解释请求回来的xml
- (void)parseDire:(CXMLDocument *) document
{
    
    [resultArray removeAllObjects];
    NSArray *items = NULL;
    NSString *nodePath=[NSString stringWithFormat:@"//%@",rootNodeName];
    NSLog(@"sladjfkjf:%@",nodePath);
    //注意，这是用XML PATH来取得元素集:
    items = [document nodesForXPath:nodePath error:nil];
    NSLog(@"mminfo解析出来的元素数: %d", items.count) ;
    for (CXMLElement *element in items)
    {
        if ([element isKindOfClass:[CXMLElement class]])
        {
            NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
            for (int i = 0; i < [element childCount]; i++)
            {
                if ([[[element children] objectAtIndex:i]
                     isKindOfClass:[CXMLElement class]])
                {
                    [item setObject:[[element childAtIndex:i] stringValue]
                             forKey:[[element childAtIndex:i] name]
                     ];
                }
            }
            //将解释出来的数据 写入数组
            NSLog(@"item :%@",[item description]);
            [resultArray addObject:item];
            
        }       
    }
    NSLog(@"the result count[%d]",resultArray.count);
    //解释成功后调用
    [self getResultArraySuccess];
    
    
}
//会在继承的类中调用
- (void)getResultArraySuccess
{
    
}

- (void)requestDidFailed:(ASIFormDataRequest *)request
{
    NSError *error = [request error];
    NSLog(@"%@",error);
    [self requestError];
}

- (void)requestError
{
    
}

- (void)alertShowWithTitle:(NSString*)title msg:(NSString*)msg cancelBtnTitle:(NSString*)cancelTitle
{
    lcalertview=[[UIAlertView alloc]initWithTitle:title message:msg delegate:self cancelButtonTitle:cancelTitle otherButtonTitles:nil, nil];
    [lcalertview show];
}

//右上角的数字设置
- (void)setTabBarItem_BadgeValue:(int)itemIndex value:(int)value
{
    [[[[self.tabBarController tabBar]items]objectAtIndex:itemIndex] setBadgeValue:[NSString stringWithFormat:@"%d",value]];
    //当数字为0时，设置为不显示
    if(value==0)
        [[[[self.tabBarController tabBar]items]objectAtIndex:itemIndex] setBadgeValue:nil];
}






#pragma mark 动画部分
-(void)moveTo:(UIView*)view x:(float)x y:(float)y
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.45];
    [UIView setAnimationDelegate:self];
    [view setCenter:CGPointMake(x,y)];
    [UIView commitAnimations];
}

-(void)moveDistance:(UIView*)view x:(float)x y:(float)y
{
    float center_x=view.center.x;
    float center_y=view.center.y;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.45];
    [UIView setAnimationDelegate:self];
    [view setCenter:CGPointMake(center_x+x,center_y+y)];
    [UIView commitAnimations];
}


@end
